package com.cloud.controller;

import com.cloud.Pay;
import com.cloud.R;
import com.cloud.lb.MyLoadBalance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @BelongsProject: my
 * @BelongsPackage: com.cloud.controller
 * @Author: ji ge
 * @CreateTime: 2024-02-02  10:37
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
public class OrderController {

    public static final String PAYMENT_URL ="http://CLOUD-PAYMENT-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private MyLoadBalance myLoadBalance;

    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping("/consumer/paymentCreate")
    public R<Pay> paymentCreate(@RequestBody Pay payment){
        return restTemplate.postForObject(PAYMENT_URL +"/paymentCreate",payment,R.class);
    }

    @GetMapping("/consumer/getPayment/{id}")
    public R<Pay> getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL+"/getPayment/"+id,R.class);
    }

    @GetMapping("/paymentQueryAllByLimit")
    public R<Pay>  paymentQueryAllByLimit(@RequestParam(defaultValue = "0") int offset,
                                          @RequestParam(defaultValue = "10") int limit){
        return restTemplate.getForObject(PAYMENT_URL+"/paymentQueryAllByLimit?offset="+offset+"&limit="+limit,R.class);
    }

    @GetMapping("/paymentGetForEntity/{id}")
    public R<Pay> paymentGetForEntity (@PathVariable("id") Long id) {
        ResponseEntity<R> forObject = restTemplate.getForEntity(PAYMENT_URL + "/getPayment/" + id, R.class);
        HttpStatus statusCode = forObject.getStatusCode();
        boolean xxSuccessful = statusCode.is2xxSuccessful();
        if (xxSuccessful) {
            return forObject.getBody();
        }else {
            return new R (444,"操作失败");
        }
    }

    @GetMapping("/paymentLb")
    public String paymentLb(){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null|| instances.isEmpty()) {
            return null;
        }
        ServiceInstance serviceInstance = myLoadBalance.instances(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri+"/paymentLb",String.class);
    }

    @GetMapping("/paymentZipkin")
    public String paymentZipkin(){
        return restTemplate.getForObject("http://localhost:8001/paymentZipkin",String.class);
    }
}
